package com.jiezuo.flowable.listener;

import com.jiezuo.common.exception.BusinessRuntimeException;
import com.jiezuo.common.utils.SpringUtil;
import com.jiezuo.flowable.mapper.CommonMapper;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.context.Context;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class PayApplyListener implements ExecutionListener {

    @Override
    public void notify(DelegateExecution execution) {
        String eventName = execution.getEventName();
        String processInstanceId = execution.getProcessInstanceId();
        RuntimeService runtimeService = Context.getProcessEngineConfiguration().getRuntimeService();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        String startUserId = processInstance.getStartUserId();//申请人

        String cost_category = (String)execution.getVariable("cost_category");
        String pay_category = (String)execution.getVariable("pay_category");
        String sub_pay_category = (String)execution.getVariable("sub_pay_category");
        String is_pay = (String)execution.getVariable("is_pay");
        
        String condition = getCondition(cost_category, pay_category, sub_pay_category, is_pay);
        execution.setVariable("condition", condition);

        setSubCondition(startUserId, condition, cost_category, pay_category, sub_pay_category, is_pay, execution);
    }

    private String getCondition(String cost_category, String pay_category, String sub_pay_category, String is_pay) {
        String condition = "";
        if(!("8".equals(cost_category) && "7".equals(sub_pay_category))){
            if("1".equals(sub_pay_category) || "3".equals(sub_pay_category) || "4".equals(sub_pay_category) || "5".equals(sub_pay_category) ||
                "7".equals(sub_pay_category)
                ){
                //付款子类别：生产类—生产类
                //付款子类别：生产类—其他
                //付款子类别：营销类—分包费用
                //付款子类别：营销类—其他
                //付款子类别：生产类—桩基类
                condition = "1";
            }
            else if("2".equals(sub_pay_category) || "9".equals(sub_pay_category)){
                //付款子类别：生产类—采购类
                //付款子类别：行政类—采购类
                condition = "2";
            }
            else if("6".equals(sub_pay_category)){
                //付款子类别：营销类—相关投标费用
                condition = "3";
            }
            else if("8".equals(sub_pay_category) || "10".equals(sub_pay_category)){
                //付款子类别：行政类—行政费用
                //付款子类别：行政类—其他
                condition = "4";
            }
            else if("11".equals(sub_pay_category)){ 
                //付款子类别：项目运营
                condition = "5";
            }
        }else{
            //表单类别：营销业务拓展费，付款单子类别：营销类—其他
            condition = "6";
        }
        return condition;
    }

    private void setSubCondition(String startUserId, String condition, 
                                    String cost_category, String pay_category, String sub_pay_category, String is_pay, 
                                    DelegateExecution execution) {
        String marketDepart = "326"; //市场营销中心
        CommonMapper commonMapper = SpringUtil.getObject(CommonMapper.class);
        Map<String,Object> map = commonMapper.findSectionByStartUserId(startUserId);
        String division = "";
        if(null != map ){
            division = String.valueOf(map.get("division"));
        }

        Map<String, String> userMap = commonMapper.findMainDeptByUserId(startUserId);
        Object mainDept = userMap.get("main_dept");
        Object deptAncestors = userMap.get("dept_ancestors");
        if(null == mainDept){
            throw new BusinessRuntimeException("用户主部门不能为空！");
        }

        if("1".equals(condition)){
            if("1".equals(division)){
                if("1".equals(sub_pay_category) || "4".equals(sub_pay_category)){
                    //付款子类别：生产类—成本类
                    //付款子类别：生产类—其他
                    execution.setVariable("subCondition", 1);
                }
                else if("5".equals(sub_pay_category) || "7".equals(sub_pay_category)){
                    //付款子类别：营销类—分包费用
                    //付款子类别：营销类—其他
                    execution.setVariable("subCondition", 2);
                }
                else if("3".equals(sub_pay_category)){
                    //付款子类别：生产类—桩基类
                    execution.setVariable("subCondition", 3);
                }
            }else{
                // if(!marketDepart.equals(mainDept.toString())){
                if(!deptAncestors.toString().contains(marketDepart)){
                    if("1".equals(sub_pay_category) || "4".equals(sub_pay_category)){
                        //付款子类别：生产类—成本类
                        //付款子类别：生产类—其他
                        execution.setVariable("subCondition", 4);
                    }
                    else if("5".equals(sub_pay_category) || "7".equals(sub_pay_category)){
                        //付款子类别：营销类—分包费用
                        //付款子类别：营销类—其他
                        execution.setVariable("subCondition", 6);
                    }
                }else{
                    if("1".equals(sub_pay_category) || "4".equals(sub_pay_category)){
                        //付款子类别：生产类—成本类
                        //付款子类别：生产类—其他
                        execution.setVariable("subCondition", 5);
                    }
                    else if("5".equals(sub_pay_category) || "7".equals(sub_pay_category)){
                        //付款子类别：营销类—分包费用
                        //付款子类别：营销类—其他
                        execution.setVariable("subCondition", 7);
                    }
                }
            }
        }
        else if("2".equals(condition)){
            if("1".equals(division)){
                execution.setVariable("subCondition", 1);
            }else{
                // if(!marketDepart.equals(mainDept.toString())){
                if(!deptAncestors.toString().contains(marketDepart)){
                    execution.setVariable("subCondition", 2);
                }else{
                    execution.setVariable("subCondition", 3);
                }
            }
        }
        else if("3".equals(condition)){
            if("1".equals(division)){
                execution.setVariable("subCondition", 1);
            }else{
                // if(!marketDepart.equals(mainDept.toString())){
                if(!deptAncestors.toString().contains(marketDepart)){
                    execution.setVariable("subCondition", 2);
                }else{
                    execution.setVariable("subCondition", 3);
                }
            }
        }
        else if("4".equals(condition)){
            if("1".equals(division)){
                execution.setVariable("subCondition", 1);
            }else{
                // if(!marketDepart.equals(mainDept.toString())){
                if(!deptAncestors.toString().contains(marketDepart)){
                    execution.setVariable("subCondition", 2);
                }else{
                    execution.setVariable("subCondition", 3);
                }
            }
        }
        else if("5".equals(condition)){
            if("1".equals(division)){
                execution.setVariable("subCondition", 1);
            }else{
                // if(!marketDepart.equals(mainDept.toString())){
                if(!deptAncestors.toString().contains(marketDepart)){
                    execution.setVariable("subCondition", 2);
                }else{
                    execution.setVariable("subCondition", 3);
                }
            }
        }
        else if("6".equals(condition)){
            if("1".equals(division)){
                execution.setVariable("subCondition", 1);
            }else{
                // if(!marketDepart.equals(mainDept.toString())){
                if(!deptAncestors.toString().contains(marketDepart)){
                    execution.setVariable("subCondition", 2);
                }else{
                    execution.setVariable("subCondition", 3);
                }
            }
        }
    }
}
